from pymongo import MongoClient

from llm.image_analysis import image_analysis

# 定义连接字符串，包含用户名和密码
uri = "mongodb://mongoadmin:maojiawei7750@123.57.232.135:27017/admin?minPoolSize=2&readPreference=primary&ssl=false"

def update_image_dialog():
    # 创建MongoDB客户端连接
    client = MongoClient(uri)

    # 选择你要使用的数据库
    db = client['test']

    collection = db['wechat_message']

    # 构建查询条件
    query = {
        "type": "image",
        "content": ""
    }
    # 执行查询并获取结果
    results = collection.find(query)

    # 遍历结果并打印每条记录
    for document in results:
        query = {
            "datetime": document['datetime'],
            "speaker": document['speaker'],
            "group_name": document['group_name']
        }
        related_dialog = collection.find(query)
        vl_result = image_analysis(related_dialog)
        print(vl_result)
        collection.update_many({
            "type": "image",
            "content": "",
            "datetime": document['datetime'],
            "speaker": document['speaker'],
            "group_name": document['group_name']
        },  # 查询条件
            {"$set": {"content": vl_result}}  # 更新操作符和要设置的新值
        )
    # 关闭数据库连接
    client.close()
